package com.sgb.kjwl.viewmodel.self.packers;

import com.sgb.kjwl.AppConstant;
import com.sgb.kjwl.api.APIManager;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.WorkerLevel;
import com.sgb.kjwl.model.entity.packers.EmploymentApplyEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.self.packers.SelfEmploymentApplyEditActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MLog;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class SelfEmploymentApplyEditModel extends BaseViewModel {

    private SelfEmploymentApplyEditActivity activity;
    private SelfRepertory selfRepertory;

    public SelfEmploymentApplyEditModel(SelfEmploymentApplyEditActivity activity, SelfRepertory selfRepertory) {
        this.activity = activity;
        this.selfRepertory = selfRepertory;
    }

    // ------------------------------------------报名详情 -----------------------------------------------

    public void getDetailEmploymentApply(String no) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getUserAPI().getDetailEmploymentApply(no).enqueue(new Callback<BaseEntity<EmploymentApplyEntity>>() {
            @Override
            public void onResponse(Call<BaseEntity<EmploymentApplyEntity>> call, Response<BaseEntity<EmploymentApplyEntity>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.updateWorkerEntity(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<EmploymentApplyEntity>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // ------------------------------------------报名请求 -----------------------------------------------
    public void employmentEditApply(EmploymentApplyEntity entity) {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().employmentApply(entity).enqueue(new Callback<BaseEntity<String>>() {
            @Override
            public void onResponse(Call<BaseEntity<String>> call, Response<BaseEntity<String>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.employmentApplySuccess();
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<String>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // ------------------------------------------工程分类 -----------------------------------------------
    public void getProTypeList() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getfindJobFirstCategory(selfRepertory.getAreaCode()).enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.showProjectCategoryPop(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // ------------------------------------------所需工种 -----------------------------------------------
    public void getWorkType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中", 0, true, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getTeamPriceAPI().getWorkType().
                enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
                    @Override
                    public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                        DialogHelper.dismissProgressDialog();
                        if (response.body() == null || response.body().getState() == null) {
                            return;
                        }
                        if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                            activity.showPickWorkTypePop(response.body().getData());
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                        DialogHelper.dismissProgressDialog();
                    }
                });
    }

    // ------------------------------------------用工类型 -----------------------------------------------
    public void getEmploymentType() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getPackerAPI().getEmploymentType().enqueue(new Callback<BaseEntity<List<WorkerLevel>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<WorkerLevel>>> call, Response<BaseEntity<List<WorkerLevel>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    List<WorkerLevel> workYears = response.body().getData();
                    List<PickSelectEntity> data = new ArrayList<>();
                    if (workYears != null && workYears.size() > 0) {
                        for (WorkerLevel workerLevel : workYears) {
                            PickSelectEntity entity = new PickSelectEntity();
                            entity.setNo(String.valueOf(workerLevel.getType()));
                            entity.setProjectscale(workerLevel.getDesc());
                            data.add(entity);
                        }
                    }
                    data.remove(0);
                    activity.showPickPackerTypePop(data);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<WorkerLevel>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    // ------------------------------------------所在地区 -----------------------------------------------
    public void getAddressModel() {
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getGoodsAPI().getCityList().enqueue(new Callback<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Response<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body().getCode().equals("200"))
                    activity.showAddressPop(response.body().getData());
            }

            @Override
            public void onFailure(Call<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
                MLog.d(AppConstant.API_LOG, "error " + t.getMessage());
            }
        });
    }
}
